# -*- coding: utf-8 -*-
import collections
import itertools
import decimal
from google.appengine.ext import db


Product = collections.namedtuple('Product', 'key description price vat units')


cc_descriptions = {
'a': (u'"The Monitor Celestra" %s, Character Class A',
decimal.Decimal('1650.00'),
decimal.Decimal('0.06'),
44),
'b': (u'"The Monitor Celestra" %s, Character Class B',
decimal.Decimal('2050.00'),
decimal.Decimal('0.06'),
74),
'c': (u'"The Monitor Celestra" %s, Character Class C',
decimal.Decimal('2650.00'),
decimal.Decimal('0.06'),
26)
}
game_keys = ['g1', 'g2', 'g3', 'g4']
game_dates = dict(zip(game_keys,
                      [u'Mar 1-3',
                       u'Mar 8-10',
                       u'Mar 15-17',
                       u'Mar 22-24']))

game_choices = [Product(g + cc,
                        cc_descriptions[cc][0] % game_dates[g],
                        cc_descriptions[cc][1],
                        cc_descriptions[cc][2],
                        cc_descriptions[cc][3])
                for g, cc in itertools.product(game_keys, 'abc')]
game_choices_d = {p.key: p for p in game_choices}

accomodation = [Product(g + '_food_hostel',
                        u'Food and hostel accomodation Friday - Sunday',
                        decimal.Decimal('750.00'),
                        decimal.Decimal('0.12'),
                        150) for g in game_keys]
accomodation_d = {p.key: p for p in accomodation}

xtra_hostel = [Product(g[0] + g[1],
                       u'Extra hostel night %s' % k,
                       decimal.Decimal('150.00'),
                       decimal.Decimal('0.12'),
                       100) for g, k in zip(itertools.product(game_keys,
                                                          ('_hostel_thursday',
                                                           '_hostel_sunday')),
                                            (u'Feb 28 - Mar 1',
                                             u'Mar 3 - Mar 4',
                                             u'Mar 7 - Mar 8',
                                             u'Mar 10 - Mar 11',
                                             u'Mar 14 - Mar 15',
                                             u'Mar 17 - Mar 18',
                                             u'Mar 21 - Mar 22',
                                             u'Mar 24 - Mar 25',))]
xtra_hostel_d = {p.key: p for p in xtra_hostel}

breakfasts = [Product(g[0] + g[1],
                      u'Breakfast %s' % k,
                      decimal.Decimal('35.00'),
                      decimal.Decimal('0.12'),
                      100) for g, k in zip(itertools.product(game_keys,
                                                        ('_breakfast_friday',
                                                         '_breakfast_monday')),
               (u'Mar 1', u'Mar 4', u'Mar 8', u'Mar 11', u'Mar 15', u'Mar 18',
                                         u'Mar 22', u'Mar 25'))]
breakfasts_d = {p.key: p for p in breakfasts}

tshirt = Product('tshirt', u'Unique Celestra souvenir T-shirt, 200 SEK',
                 decimal.Decimal('200.00'),
                 decimal.Decimal('0.25'),
                 600)

cfbonus = Product('cfbonus', u'Extra perk for early contribution',
                  decimal.Decimal('0'),
                  decimal.Decimal('0.06'),
                  600)


def install_products():
    import signup
    assert not list(signup.Product.all())
    db.put([signup.Product(key_name=p.key,
                           description=p.description,
                           price=p.price,
                           vat=p.vat,
                           units_available=p.units)
            for p in itertools.chain(game_choices,
                                     accomodation,
                                     xtra_hostel,
                                     breakfasts,
                                     [tshirt, cfbonus])])


def get_cc_product(order):
    return next((i.product.key().name()
                 for i in order.items
                 if i.product.key().name() in game_choices_d), None)


def get_hostel_products(order):
    for i in order.items:
        name = i.product.key().name()
        if name in xtra_hostel_d:
            yield name


def get_breakfast_products(order):
    for i in order.items:
        name = i.product.key().name()
        if name in breakfasts_d:
            yield name


def get_tshirt(order):
    return next((True for i in order.items
                 if i.product.key().name() == 'tshirt'), False)


# Game 1: Mar 1-3, 2013, Character Class A
# Game 1: Mar 1-3, 2013, Character Class B
# Game 1: Mar 1-3, 2013, Character Class C
# Game 1: Mar 1-3, 2013, Character Class D
#
# Game 2: Mar 8-10, 2013, Character Class A
# Game 2: Mar 8-10, 2013, Character Class B
# Game 2: Mar 8-10, 2013, Character Class C
# Game 2: Mar 8-10, 2013, Character Class D
#
# Game 3: Mar 15-17, 2013, Character Class A
# Game 3: Mar 15-17, 2013, Character Class B
# Game 3: Mar 15-17, 2013, Character Class C
# Game 3: Mar 15-17, 2013, Character Class D
#
# Game 4: Mar 22-24, 2013, Character Class A
# Game 4: Mar 22-24, 2013, Character Class B
# Game 4: Mar 22-24, 2013, Character Class C
# Game 4: Mar 22-24, 2013, Character Class D

# Game 1: Mar 1-3, 2013, Food and Hostel
# Game 2: Mar 8-10, 2013, Food and Hostel
# Game 3: Mar 15-17, 2013, Food and Hostel
# Game 4: Mar 22-24, 2013, Food and Hostel


# Extranatt 28-1
# Extranatt 3-4
# Extranatt 7-8
# Extranatt 10-11
# Extranatt 14-15
# Extranatt 17-18
# Extranatt 21-22
# Extranatt 24-25
#
# Frukost 1
# Frukost 4
# Frukost 7
# Frukost 11
# Frukost 15
# Frukost 18
# Frukost 22
# Frukost 25
